package com.chengh.nami.web.controller.monitor;

import com.chengh.nami.core.annotation.SysLog;
import com.chengh.nami.common.constants.CacheConstants;
import com.chengh.nami.common.domain.model.LoginUserEntity;
import com.chengh.nami.common.enums.system.BusinessType;
import com.chengh.nami.common.response.PageData;
import com.chengh.nami.common.response.R;
import com.chengh.nami.common.utils.StringUtils;
import com.chengh.nami.common.utils.redis.RedisCache;
import com.chengh.nami.system.domain.SysUserOnline;
import com.chengh.nami.system.service.ISysUserOnlineService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/monitor/online")
@Tag(name = "SysUserOnlineController", description = "在线用户管理")
public class SysUserOnlineController {

    private final RedisCache redisCache;

    private final ISysUserOnlineService userOnlineService;

    /**
     * 查询所有的在线用户信息
     *
     * @return 所有的在线用户信息
     */
    @GetMapping("/list")
    @Operation(summary = "查询所有的在线用户列表", description = "返回的是全量信息,由前端进行分页")
    public R<PageData<SysUserOnline>> list(String ipaddr, String userName) {
        Collection<String> keys = redisCache.keys(CacheConstants.LOGIN_TOKEN_KEY + "*");
        log.info("所有的在线用户缓存列表: {}", keys);
        ArrayList<SysUserOnline> userOnlineList = new ArrayList<>();
        for (String key : keys) {
            LoginUserEntity userEntity = redisCache.getCacheObject(key);
            if (StringUtils.isNotEmpty(ipaddr) && StringUtils.isNotEmpty(userName)) {
                userOnlineList.add(userOnlineService.selectOnlineByInfo(ipaddr, userName, userEntity));
            } else if (StringUtils.isNotEmpty(ipaddr)) {
                userOnlineList.add(userOnlineService.selectOnlineByIpaddr(ipaddr, userEntity));
            } else if (StringUtils.isNotEmpty(userName) && !StringUtils.isNull(userEntity.getUser())) {
                userOnlineList.add(userOnlineService.selectOnlineByUserName(userName, userEntity));
            } else {
                SysUserOnline sysUserOnline = userOnlineService.loginUserEntityToUserOnline(userEntity);
                userOnlineList.add(sysUserOnline);
            }
        }
        // 逆排序
        Collections.reverse(userOnlineList);
        // 删除所有的null元素
        userOnlineList.removeAll(Collections.singleton(null));
        PageData<SysUserOnline> pageData = PageData.build(userOnlineList);
        return R.ok(pageData);
    }

    /**
     * 强退用户
     *
     * @param tokenId tokenId
     * @return 是否成功
     */
    @SysLog(title = "在线用户", businessType = BusinessType.FORCE)
    @DeleteMapping("/{tokenId}")
    public R<Void> forceLogout(@PathVariable String tokenId) {
        return R.bool(redisCache.deleteObject(CacheConstants.LOGIN_TOKEN_KEY + tokenId));
    }
}
